/****************************************************
    ScriptName:        Boss1.cs
    Author:            摸鱼程序男
    CreatTime:    2020/12/22 22:44:49
    Description:     Nothing
*****************************************************/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using DG.Tweening;
using System.Linq;
using BTAI;
using ZYKTool;
using ZYKTool.Timer;
using LongZhiJie;
namespace HQL
{
    public class Boss1 : BossBase
    {
        protected int range_dash;
        protected int timeRange;

        public Transform[] Pos;
        public GameObject 子弹;




        public override void Init()
        {

            base.Init();
            _aiRoot.OpenBranch(
                BT.If(CanAttack).OpenBranch(
                    BT.Call(Attack)
                )
            /*
           BT.While(die1).OpenBranch(
                 BT.Wait(1),
                 BT.Call(换阶段)
                  )
            */
            );
            _SR = transform.GetOrAddComponent<SpriteRenderer>();
            _MyColor = _SR.color;
            // ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask(换阶段, 1, 1, PETimeUnit.Second);
            _AttackTimer =1.5f;
   
            _Blood = TapTapStaticValue.BossAttributes[BossName.Boss1].Blood;
            BossName = BossName.Boss1;
        }
        #region 攻击类
        protected void time_limit()
        {
            if (_已经死亡) return;
            int limitGo = range_dash;
            // Debug.LogError(range_dash);
            //range_dash = UnityEngine.Random.Range(0, 4);
            bool temp = UnityEngine.Random.Range(0, 2) > 0;
            if (limitGo == 2) range_dash = temp ? 1 : 3;
            if (limitGo == 0) range_dash = temp ? 1 : 3;
            if (limitGo == 3) range_dash = temp ? 0 : 2;
            if (limitGo == 1) range_dash = temp ? 0 : 2;
            /*
             *  3 2
             *  0 1
             *  
             */
            _SR.color = Color.blue;
            _AttackOver=true;
        }
        public override void 换阶段()
        {
            base.换阶段();
            //Debug.LogError("二级模式切换阶段！");
            _AttackTimer = 0.5f;
     
            //todo:贺启亮根据血量 让他换阶段就行
        }
        private void Attack1()
        {
            if (_已经死亡) return;
            //冲撞
            _SR.color = _MyColor;
            if (UnityEngine.Random.Range(0, 10) > 4)
            {
              //  Debug.LogError(44444444);
                ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                    (() =>
                    {
                        //   Debug.LogError("dash");
                        dash(Pos[range_dash]);
                    },
                     0.5f, 1, PETimeUnit.Second);//0.5s后撞击
            }
            else
            {
                //发射子弹
              //  Debug.LogError(555555555);
                int damage= UnityEngine.Random.Range(1, 2);
                ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                    (() =>
                    {
                        Shot(damage>2?2:1, 150);
                    },
                       damage , 1, PETimeUnit.Second);//三到五秒才射击
            }


        }
        protected void dash(Transform target)
        {
            if (_已经死亡) return;
            //transform.position = Vector3.MoveTowards( transform.position, new Vector3(p, x, 0), 2.4f);
            PlaySound();
            transform.DOKill();
            // Debug.LogError("dash");
            transform.DOMove(target.position, 0.5f);
            ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
           (time_limit,
            0.6f, 1, PETimeUnit.Second);//停止撞击
        }
        protected void Shot(int damage, float speed)
        {
            if (_已经死亡) return;
            //Debug.LogError("shot");
            PlaySound();
            Vector3 dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
            GameObject bullet = Instantiate(子弹, transform.position, Quaternion.Euler(dir));
            BossBullet bossbullet = bullet.GetOrAddComponent<BossBullet>();
            bossbullet.Damage = damage;
            bossbullet.speed = speed;
            bossbullet.dir = (TapTapStaticValue.Player.transform.position - transform.position).normalized;
            _AttackOver = true;
            _SR.color = _MyColor;
        }
        private void Attack2()
        {
            //Debug.LogError(66);
            if (_已经死亡) return;
           // Debug.LogError("attack2");

                _SR.color = Color.blue;
                if (UnityEngine.Random.Range(0, 10) > 1)
                {

                    ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                        (() =>
                        {
                            if (_已经死亡) return;
                       //      Debug.LogError("dash");
                            dash(Pos[range_dash]);
                        },
                         0.5f, 1, PETimeUnit.Second);//0.5s后撞击
                }
                else
                {
                    //发射子弹
                    float damage = UnityEngine.Random.Range(0.5f, 1f);
                    ZYKTimerSystemTool.Instance.ZYKTimerAddTimerTask
                        (() =>
                        {
                            if (_已经死亡) return;
                           // Debug.LogError("shoot");
                            Shot(damage > 1 ? 2 : 1, 200);
                        },
                           damage, 1, PETimeUnit.Second);//三到五秒才射击
                }
            
           


        }
        protected override void Attack()
        {
            if (_已经死亡) return;
            //Debug.LogError("attack");
            switch (当前阶段)
            {
                case 1:
                    Attack1();
                    break;
                case 2:
                    Attack2();
                    //TODO:贺启亮 换阶段之后如果需要改变参数写这里
                    break;
            }
        }
        #endregion
        public override void BossHunt(float damage)
        {
            TapTapStaticValue.TapTapModelManger.Damage(BossName.Boss1, damage);
            _Blood = TapTapStaticValue.BossAttributes[BossName.Boss1].Blood;
            //Debug.LogError(TapTapStaticValue.BossAttributes[BossName.Boss1].Blood);
            if(TapTapStaticValue.BossAttributes[BossName.Boss1].Blood <= 0)
            {
                PlaySound(2);
                Die(BossName.Boss1);
                TapTapStaticValue.TapTap升级系统.TapTapStartUpdate();
            }
            else if (TapTapStaticValue.BossAttributes[BossName.Boss1].Blood <= 100 && 当前阶段 < 2)
            {
                换阶段();
            }
        }
   


    }
}